package com.Controller.servlet;

import com.Model.DengLuModel.PanDuanZhangHao;
import com.Model.Pojo.LoginRecord;
import com.Model.Pojo.Users;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class DengLu extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		response.setContentType("text/html");//设置参数传递编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		HttpSession session = request.getSession(false);//获取session
		String name = request.getParameter("userid"); //获取账号密码身份信息
		String pass = request.getParameter("passwordinput");
		String shenfen = request.getParameter("shenfen");
		String url = request.getParameter("url");
		LoginRecord loginRecord = new LoginRecord();
		loginRecord.setLogin_IP(loginRecord.getIpAddr(request)); //获取用户IP存入登录记录中

		//实例化判断账号密码的对象
		PanDuanZhangHao panduan = new PanDuanZhangHao(name, pass, shenfen);
		String flag = panduan.panDuan(); //第二次判断账号密码是否正确
		if(flag.equals("false")){
			response.sendRedirect(url); //返回之前的页面。
		}else{
			Users users = panduan.getUsersByNameOrShoujihaoOrYouxiang(flag,name,pass);//根据对应的输入的信息获取用户的详细信息
			//System.out.println(users.getZhanghao()+users.getMima()+users.getYue()+users.getMoney());
			session.setAttribute("users", users); //把user存在session里面
			loginRecord.setUserLoginRecord(users.getZhanghao(), shenfen);//该用户的登录记录写入数据库中
			response.sendRedirect(url); //返回之前的页面。
		}
	}
}